﻿using System.Xml;
using UnityEngine;

namespace PreGUI.Interfaces
{
    /// <summary>
    /// Base interface that all PreGui controls must inherit.
    /// </summary>
    public interface IPGControl 
    {
        /// <summary>
        /// Lays out the controls onto the parent GameObject.
        /// </summary>
        /// <param name="gui">The base Gui system.</param>
        /// <param name="parent">The GameObject of the parent that will hold this control.</param>
        /// <param name="windowName">The name of the window this control belongs to.</param>
        /// <returns>The base GameObject of the control that was constructed.</returns>
        GameObject LayoutControls(PreGui gui, GameObject parent, string windowName);

        /// <summary>
        /// Generates the control from an xml node.
        /// </summary>
        /// <param name="node">The xml node that contains information necessary to generate this control.</param>
        /// <returns>A constructed instance of a PreGui object based on the xml node information.</returns>
        IPGControl GenerateFromXmlNode(XmlNode node);
    }

    /// <summary>
    /// Helper class with extensions for processing and building controlls.
    /// </summary>
    public static class IPGControlExtensions
    {
        /// <summary>
        /// Processes a collection of controls for a parent control.
        /// </summary>
        /// <param name="children">The array of child controls this control has.</param>
        /// <param name="gui">The base PreGui object.</param>
        /// <param name="parent">The control that will contain the children.</param>
        /// <param name="windowName">The name of the window this control is a member of.</param>
        public static void ProcessControls(this IPGControl[] children, PreGui gui, GameObject parent, string windowName)
        {
            if (children == null)
                return;

            foreach(var child in children)
            {
                if (child == null)
                    continue;

                child.LayoutControls(gui, parent, windowName);
            }
        }
    }
}